<?php

namespace app\controllers;

use Yii;
use app\models\TestAccount;
use app\models\TestAccountSearch;
use app\models\Project;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * TestAccountController implements the CRUD actions for TestAccount model.
 */
class TestAccountController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all TestAccount models.
     * @return mixed
     */
    public function actionIndex($pro_id=null)
    {
        $searchModel = new TestAccountSearch();
        $queryParams = array_merge(array(),Yii::$app->request->getQueryParams());
		
		if($pro_id!=null)
		{
			$queryParams['TestAccountSearch']['pro_id'] = $pro_id;
		}
        $dataProvider = $searchModel->search($queryParams);

        return $this->render(Yii::$app->user->identity->acc_type==0?'index':'index_user', [
            'pro_id' => $pro_id,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single TestAccount model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new TestAccount model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($pro_id=null)
    {
        $model = new TestAccount();
        $modelProject = new Project();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
			$model->pro_id = $pro_id;
            return $this->render('create', [
                'model' => $model,
				'arrayProject' => ProjectController::getProject(),
            ]);
        }
    }

    /**
     * Updates an existing TestAccount model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $modelProject = new Project();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
				'arrayProject' => ProjectController::getProject(),
            ]);
        }
    }

    /**
     * Deletes an existing TestAccount model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the TestAccount model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return TestAccount the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = TestAccount::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
